<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\STeacherInfo as TeacherModel;
use App\Models\STeaScheduleInfo as TeaScheduleModel;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;

class TeacherController extends BaseController
{
    //获取外教列表
    public function getAllTeacher()
    {
    	$teacher_type = request("teacherType", 1);
    	$start        = request("start", 0);
    	// dd($teacher_type);
    	$list = TeacherModel::getList($teacher_type, $start,5);

    	if($list->isEmpty()) {
    		return $this->outPutErr("没有更多了！");
    	} 

    	return $this->outPutSucc($list);
    }

    //外教详情
    public function getTeacherDetail()
    {
    	$teacher_id = intval( request("teacherId") );
    	// $user_id = 1;
        $user_id = auth()->user()->id;
    	if($teacher_id<1) {
			return $this->outPutErr("请输入有效的teacherId!");
    	}

    	$ret = TeacherModel::getTeacherInfoForUser($teacher_id, $user_id);

    	if($ret) {
    		return $this->outPutSucc($ret);
    	}
		return $this->outPutErr("没有查到外教信息！");
    }

    //外教时间
    public function getTeaSchedule()
    {
        $teacher_id = intval( request("teacherId") );
        $date_time  = request("dateTime");
        if($teacher_id<1) {
            return $this->outPutErr("请输入有效的teacherId!");
        }

        if(!$date_time) {
            $date_time = now()->toDateString();
        } else {
            $date_time = Carbon::parse($date_time)->toDateString();
        }

        $tea_info = TeacherModel::getTeacherInfoForAppoint($teacher_id);

        $tea_schedule = TeaScheduleModel::getTeaScheduleByDay($teacher_id, $date_time);

        if(!$tea_info) {
            return $this->outPutErr("未查询到外教信息!");
        }

        return $this->outPutSucc([
            'tea_info'     => $tea_info,
            'tea_schedule' => $tea_schedule?:'暂未设置排课信息'
        ]);
    }

    //约课外教
    public function appointTea()
    {
        $teacher_id = intval( request("teacherId") );

    }


    //老师web登陆
    public function login()
    {
        $email    = request('email', '');
        $password = request('password', '');

        if($email === '' || $password === '') {
            return $this->outPutErr("邮箱或密码不能为空");
        }

        $teacher = TeacherModel::where('email', $email)->first();

        if($teacher) {
            if( Hash::check($password, $teacher->password) ) {

                Auth::login($teacher, true);

                return redirect("/");

            } else {
                return $this->outPutErr('邮箱或密码错误！');
            }
        }

        return $this->outPutErr('用户不存在！');
    }

    //老师退出
    public function logout()
    {
        Auth::logout();
        return redirect("/teacher/login");
    }
}
